package com.gao;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.Test;

public class Autu {
    private static String author = "GaoHuoHuo";
    //生成位置
    private static String outPath = "D:\\Logger\\MP_AutoGenerator\\";
    //驱动
    private static String driver = "com.mysql.jdbc.Driver";
    //链接路径
    private static String url = "jdbc:mysql://127.0.0.1:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
    //数据库用户名
    private static String username = "root";
    //数据库密码
    private static String password = "123";
    //数据库表的前缀
    private static String tablePrefix = "tb_";
    //需要生成的表名
    private static String[] tables = {"tb_user"};
    //顶级包结构
    private static String parentPackage = "com.gao";
    //数据访问层包名
    private static String dao = "mapper";
    //业务层包名
    private static String service = "service";
    //实体层包名
    private static String entity = "pojo";
    //控制层包名
    private static String controller = "controller";
    //mapper映射文件包名
    private static String mapperxml = "mapper";

    @Test
    public void testCreat() {
        //log.debug("Logger level : {}","debug");
        //1.全局配置
        GlobalConfig config = new GlobalConfig ();
        config.setAuthor ( author )//作者
                .setOutputDir ( outPath )//生成路径
                .setFileOverride ( true )//文件覆盖
                .setIdType ( IdType.AUTO )//主键策略
                .setServiceName ( "%sService" )//设置生成的Service接口名字的首字母是否为I，加上%s则不生成I
                .setBaseResultMap ( true )//映射文件中是否生成ResultMap配置
                .setBaseColumnList ( true );//生成通用sql字段
        //2.数据源配置
        DataSourceConfig dsConfig = new DataSourceConfig ();
        dsConfig.setDbType ( DbType.MYSQL )//设置数据库类型
                .setDriverName ( driver )//设置驱动
                .setUrl ( url )//设置连接路径
                .setUsername ( username )//设置用户名
                .setPassword ( password );//设置密码
        //3.策略配置
        StrategyConfig strategyConfig = new StrategyConfig ();
        strategyConfig.setCapitalMode ( true )//全局大写命名
                .setNaming ( NamingStrategy.underline_to_camel )//数据库表映射到实体的命名策略
                .setTablePrefix ( tablePrefix )//表前缀
                .setInclude ( tables )//生成的表
                .setEntityLombokModel ( true );
        //4.包名策略
        PackageConfig packageConfig = new PackageConfig ();
        packageConfig.setParent ( parentPackage )//顶级包结构
                .setMapper ( dao )//数据访问层
                .setXml ( mapperxml )//mapper映射文件
                .setService ( service )//业务逻辑层
                .setController ( controller )//控制器
                .setEntity ( entity );//实体类
        //5.综合配置
        AutoGenerator autoGenerator = new AutoGenerator ();
        autoGenerator.setGlobalConfig ( config )
                .setDataSource ( dsConfig )
                .setStrategy ( strategyConfig )
                .setPackageInfo ( packageConfig ).setTemplateEngine ( new BeetlTemplateEngine () );
        // .setTemplateEngine ( new FreemarkerTemplateEngine ());
        //6.执行
        autoGenerator.execute ();
    }
}    